package callnumber.gtdev5.com.photoVault.adapter;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.design.widget.Snackbar;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.gtdev5.geetolsdk.mylibrary.util.ToastUtils;

import org.litepal.crud.DataSupport;

import java.util.ArrayList;
import java.util.List;

import callnumber.gtdev5.com.photoVault.R;
import callnumber.gtdev5.com.photoVault.activity.PhotoActivity;
import callnumber.gtdev5.com.photoVault.activity.VideoActivity;
import callnumber.gtdev5.com.photoVault.bean.FileNameBean;
import callnumber.gtdev5.com.photoVault.bean.VideoBean;

/********************
 *  Created by ：  lyq 
 *         Date：  2019\1\22 0022.
 * Introduction：   
 *********************/

public class VidoeAdapter extends RecyclerView.Adapter<VidoeAdapter.MyViewHolder> implements View.OnClickListener {
    private Context context;
    private List<String> list;
    private MyViewHolder holder;
    private List<VideoBean> pName;
    private int VISIBLE = 0;
    private int ISCHOOSE = 0;
    private int isshow = 0;
    private VideoBean phNameBean;
    private int isSelect;

    public VidoeAdapter(Context context, List<String> list) {
        this.context = context;
        this.list = list;


    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View inflate = LayoutInflater.from(context).inflate(R.layout.item_home, null, true);
        holder = new MyViewHolder(inflate);
        holder.mCheckBox.setVisibility(View.GONE);
        inflate.setOnClickListener(this);
        return holder;
    }
 
    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.tv.setText(list.get(position));
        holder.mCheckBox.setVisibility(View.GONE);
        if (isshow == VISIBLE) {
            holder.mCheckBox.setVisibility(View.GONE);
        } else {
            if (list.size() <= 4) {
                ToastUtils.showShortToast("默认文件夹无法删除");
                return;
            } else
                holder.mCheckBox.setVisibility(View.VISIBLE);
        }
        holder.photo_one.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pName = new ArrayList<>();
                pName = DataSupport.findAll(VideoBean.class);
                Intent intent = new Intent(context, VideoActivity.class);
                intent.putExtra("ids", pName.get(position).getId());
                context.startActivity(intent);
            }
        });
        pName = new ArrayList<>();
        pName = DataSupport.findAll(VideoBean.class);
        if (pName != null && pName.size() > 0) {
            switch (pName.get(position).getNum()) {
                case 0:
                    holder.photo_one.setBackgroundDrawable(context.getResources().getDrawable(R.mipmap.morenxiangce1));
                    break;
                case 1:
                    holder.photo_one.setBackgroundDrawable(context.getResources().getDrawable(R.mipmap.morenxiangce2));
                    break;
                case 2:
                    holder.photo_one.setBackgroundDrawable(context.getResources().getDrawable(R.mipmap.morenxiangce3));
                    break;
                case 3:
                    holder.photo_one.setBackgroundDrawable(context.getResources().getDrawable(R.mipmap.morenixangce4));
                    break;
            }
        } else {
            switch (position) {
                case 0:
                    holder.photo_one.setBackgroundDrawable(context.getResources().getDrawable(R.mipmap.morenxiangce1));
                    break;
                case 1:
                    holder.photo_one.setBackgroundDrawable(context.getResources().getDrawable(R.mipmap.morenxiangce2));
                    break;
                case 2:
                    holder.photo_one.setBackgroundDrawable(context.getResources().getDrawable(R.mipmap.morenxiangce3));
                    break;
                case 3:
                    holder.photo_one.setBackgroundDrawable(context.getResources().getDrawable(R.mipmap.morenixangce4));
                    break;
            }
        }
        holder.mCheckBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (position == 0) {
                    Snackbar.make(v, "此条目不能删除", Snackbar.LENGTH_SHORT).show();
                } else if (position == 1) {
                    Snackbar.make(v, "此条目不能删除", Snackbar.LENGTH_SHORT).show();
                } else if (position == 2) {
                    Snackbar.make(v, "此条目不能删除", Snackbar.LENGTH_SHORT).show();
                } else if (position == 3) {
                    Snackbar.make(v, "此条目不能删除", Snackbar.LENGTH_SHORT).show();
                } else {
                    AlertDialog.Builder builder = new AlertDialog.Builder(context);
                    builder.setTitle("删除该视频文件?");
                    builder.setMessage("您是否要删除该文件?（注意！删除后无法恢复！）");
                    builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            removeData(position);

                        }
                    });
                    builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {

                        }
                    });
                    builder.show();

                }
            }
        });
    }

    /**
     * 是否显示选择筐
     */
    public void setMode(int isshow) {
        this.isshow = isshow;
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    int num;

    //  添加数据
    public void addData(int position, String newName) {
//      在list中添加数据，并通知条目加入一条
        pName = DataSupport.findAll(VideoBean.class);
        num = pName.get(position - 1).getNum();
        list.add(position, newName);
        //添加动画
        VideoBean phNameBean = new VideoBean();
        phNameBean.setName(newName);
        if (num >= 3) {
            phNameBean.setNum(0);
            num = 0;
        } else {
            num++;
            phNameBean.setNum(num);
        }
        phNameBean.setIsSelect(0);
        phNameBean.save();
        notifyItemInserted(position);
        getdata();
    }

    public void getdata() {
        pName = DataSupport.findAll(VideoBean.class);
    }
    List<FileNameBean> newsList ;
    //  删除数据
    public void removeData(int position) {
        getdata();
        DataSupport.delete(VideoBean.class, pName.get(position).getId());
        newsList = new ArrayList<>();
        newsList = DataSupport.where("ids = ?", pName.get(position).getId()+"").find(FileNameBean.class);
        if (newsList != null && !newsList.isEmpty()) {
            DataSupport.deleteAll(FileNameBean.class, "ids = ?", pName.get(position).getId() + "");
        }
        list.remove(position);
        //删除动画
        notifyItemRemoved(position);
        notifyDataSetChanged();
    }

    @Override
    public void onClick(View v) {
        if (mItemClickListener != null) {
            mItemClickListener.onItemClick((Integer) v.getTag());
        }
    }

    public OnItemClickListener mItemClickListener;

    public interface OnItemClickListener {
        void onItemClick(int position);
    }

    /**
     * ViewHolder的类，用于缓存控件
     */
    class MyViewHolder extends RecyclerView.ViewHolder {
        TextView tv, tv_delete;
        RelativeLayout photo_one;
        ImageView mCheckBox;

        //因为删除有可能会删除中间条目，然后会造成角标越界，所以必须整体刷新一下！
        public MyViewHolder(View view) {
            super(view);
            tv = (TextView) view.findViewById(R.id.id_num);
            tv_delete = (TextView) view.findViewById(R.id.tv_delete);
            photo_one = view.findViewById(R.id.photo_one);
            mCheckBox = view.findViewById(R.id.check_box);
        }
    }
}
